//
//  NHCourseDetailVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/2/22.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "NHCourseDetailVC.h"
#import "HElCourseIntroVC.h"
#import "HELCourseHeader.h"
#import "HElCourseIntroVC.h"
#import "HELCourseContentsVC.h"
#import "HELCourseCommentVC.h"
#import "SegmentViewController.h"
#import "HELCourseHeader.h"
#import "HELCourseDetailBottom.h"
#import "Masonry.h"
#import "HELCourseDetailBottom.h"
#import "HELCourseDetailModel.h"
#import "HELOrderConfirmVC.h"
#import "JCAlertView.h"
//#import "TYVideoPlayerController.h"
#import "MoviePlayerViewController.h"
#import "ReaderFileViewController.h"
#import "HELService.h"
#import "HECourseDetailScrollView.h"
#import "OrderServer.h"
@interface NHCourseDetailVC ()<UIScrollViewDelegate>
@property (nonatomic,  strong) HElCourseIntroVC *introVC;
@property (nonatomic,  strong) HELCourseContentsVC *contentsVC;
@property (nonatomic,  strong) HELCourseCommentVC *commentVC;
@property (nonatomic,  strong) HELCourseDetailBottom *bottomView;
@property (nonatomic,  strong) HELCourseHeader *headerView;

@property (nonatomic,  strong) HELCourseDetailModel *courseDetail;

@property (nonatomic,  assign) BOOL canScroll;

@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabView;

@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabViewPre;

@property (nonatomic,  strong) HECourseDetailScrollView *scrollview;


@end

@implementation NHCourseDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUp];
    
    [self getCourseDetail];
    self.title = @"课程详情";
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getCourseDetail) name:RefreshCourseDetail object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kLeaveTopNotificationName object:nil];
}


-(void)acceptMsg : (NSNotification *)notification{
    //NSLog(@"%@",notification);
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if ([canScroll isEqualToString:@"1"]) {
        self.canScroll = YES;
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat tabOffsetY = CGRectGetMaxY(self.headerView.frame);
    CGFloat offsetY = scrollView.contentOffset.y;
    
//    NSLog(@"----%.3f--------%.3f",tabOffsetY,offsetY);
    _isTopIsCanNotMoveTabViewPre = _isTopIsCanNotMoveTabView;
    if (offsetY>=tabOffsetY) {
        scrollView.contentOffset = CGPointMake(0, tabOffsetY);
        _isTopIsCanNotMoveTabView = YES;
    }else{
        _isTopIsCanNotMoveTabView = NO;
    }
    if (_isTopIsCanNotMoveTabView != _isTopIsCanNotMoveTabViewPre) {
        if (!_isTopIsCanNotMoveTabViewPre && _isTopIsCanNotMoveTabView) {
            //NSLog(@"滑动到顶端");
            [[NSNotificationCenter defaultCenter] postNotificationName:kGoTopNotificationName object:nil userInfo:@{@"canScroll":@"1"}];
            _canScroll = NO;
        }
        if(_isTopIsCanNotMoveTabViewPre && !_isTopIsCanNotMoveTabView){
            //NSLog(@"离开顶端");
            if (!_canScroll) {
                scrollView.contentOffset = CGPointMake(0, tabOffsetY);
            }
        }
    }
}

- (void)dealloc{
  
    [[NSNotificationCenter defaultCenter] removeObserver:self];
 
}

- (HELCourseDetailBottom *)bottomView{
    if (!_bottomView) {
        _bottomView = [[HELCourseDetailBottom alloc] init];
        weakSelf_define;
        _bottomView.joinBlock = ^(){
            
            if (![weakSelf.courseDetail.status containsString:@"未参加"] && weakSelf.courseDetail.subFileName) {
                
                if ([weakSelf.courseDetail.subType integerValue] == 1) {
                    //视屏
                    MoviePlayerViewController *vc =[MoviePlayerViewController new];
                    
                    vc.videoURL = [NSURL URLWithString:weakSelf.courseDetail.subFileUrl];

                    
                    //
              
                    __block NSDate *date =[NSDate date];
                    vc.didDismissBLock = ^(){
                        [HELService uploadVedioProgress:date withcourseId:[weakSelf.courseDetail.id integerValue] withContentId:[weakSelf.courseDetail.subCourseId integerValue] couserStatus:weakSelf.courseDetail.status withContent:1];
                      
                    };
                  
                    
                    [weakSelf presentViewController:vc animated:YES completion:nil];
                }else if ([weakSelf.courseDetail.subType integerValue] == 2){
                    //PDF
                    ReaderFileViewController *fileViewController = [[ReaderFileViewController alloc] init];
                    //
                    //
                    fileViewController.title = weakSelf.courseDetail.subFileName;
                    
                    HELCourseContentModel *model = [HELCourseContentModel new];
                    model.fileName = weakSelf.courseDetail.subFileName;
                    model.fileUrl = weakSelf.courseDetail.subFileUrl;
                    model.type = [@(2) stringValue];
                    fileViewController.pdfModel = model;
                    
                    fileViewController.modalPresentationStyle = UIModalPresentationFullScreen;
                  
                    fileViewController.callBack = ^(NSUInteger maxPage){
                       
                        [HELService uploadPdfPage:maxPage withcourseId:[weakSelf.courseDetail.id integerValue]  withContentId:[weakSelf.courseDetail.subCourseId integerValue] couserStatus:weakSelf.courseDetail.status  withContent:1];
                        
                    };
                    
                    [weakSelf presentViewController:fileViewController animated:YES completion:NULL];
                 
                }

                
            }else{
                [JCAlertView showTwoButtonsWithTitle:nil Message:@"是否确认参加课程" ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:@"点错了~" Click:^{
                    
                } ButtonType:JCAlertViewButtonTypeMainColor ButtonTitle:@"立即前往" Click:^{
                    
                    
                    if ([weakSelf.courseDetail.coursePrice floatValue] <=0) {
                          [[OrderServer new] createOrder:weakSelf.courseId withView:weakSelf.view];
                    }else{
                        HELOrderConfirmVC *vc =[HELOrderConfirmVC new];
                        vc.courseId = [weakSelf.courseDetail.id integerValue];
                        [weakSelf.navigationController pushViewController:vc animated:YES];
                    }
              
                    
                }];
            }
            
         
          
        };
      
    }
    return _bottomView;
}
- (HELCourseHeader *)headerView{
    if (!_headerView) {
        _headerView = [HELCourseHeader sharedHeader];
        weakSelf_define;
        _headerView.priseBlock = ^(BOOL is){
            [weakSelf getCourseDetail];
        };
        _headerView.collectBlock = ^(BOOL is){
            [weakSelf getCourseDetail];
        };
        
    }
    return _headerView;
    
}
- (HElCourseIntroVC *)introVC{
    if (!_introVC) {
        _introVC = [HElCourseIntroVC new];
        
        
    }
    return _introVC;
}
- (HELCourseContentsVC *)contentsVC{
    if (!_contentsVC) {
        _contentsVC = [HELCourseContentsVC new];
        _contentsVC.courseDetail = self.courseDetail;
        
    }
    return _contentsVC;
}
- (HELCourseCommentVC *)commentVC{
    if (!_commentVC) {
        _commentVC = [HELCourseCommentVC new];
        
    }
    return _commentVC;
}

- (void)viewWillDisappear:(BOOL)animated{

}
- (void)loadView{
    
    UIView *view =[UIView new];
    view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 );
    self.view = view;
    
    HECourseDetailScrollView * scrollview =[[HECourseDetailScrollView alloc] init];
    scrollview.frame = CGRectMake(0,0, kScreenWidth, kScreenHeight - 64 - 49);
    [self.view addSubview:scrollview];
    scrollview.delegate = self;
    self.scrollview = scrollview;
    
    
 
}

- (void)setUp{
    
    
    CGFloat f = kScreenFactor;
    self.headerView.frame = CGRectMake(0, 0, kScreenWidth, MHeaderViewHeight + (f-1)* 168);
    [self.scrollview addSubview:self.headerView];
    

    self.bottomView.frame = CGRectMake(0, kScreenHeight - 49 - 64, kScreenWidth, 49);
    [self.view addSubview:self.bottomView];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    SegmentViewController *vc = [[SegmentViewController alloc]init];
    NSArray *titleArray = @[@"简介", @"目录",@"评价"];
    vc.titleArray = titleArray;
    vc.titleSelectedColor = kMainColor;
    vc.subViewControllers = @[self.introVC,self.contentsVC, self.commentVC];
    vc.buttonHeight = 49;
    vc.bottomLineHeight = 4;
    vc.bottomLineColor = kMainColor;
    vc.buttonWidth = kScreenWidth/3;
    //    vc.segmentHeaderType = 0;
    [vc initSegment];
    [vc addParentController:self withPatientView:self.scrollview];
    vc.view.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), kScreenHeight, kScreenHeight-64);
    
    [self.scrollview setContentSize:CGSizeMake(kScreenWidth, kScreenHeight- 64 + MHeaderViewHeight + (f-1)* 168 - 49)];
    weakSelf_define;
    vc.didEnterDifferentVC = ^(NSInteger index){
        switch (index) {
            case 0:
                
                break;
            case 1:
                [weakSelf.contentsVC firstGetData];
                break;
            case 2:
                 [weakSelf.commentVC firstGetData];
                break;
                
            default:
                break;
        }
    };
   
}

- (void)viewDidLayoutSubviews{
   
    
    [super viewDidLayoutSubviews];
}

- (void)getCourseDetail{
    

    if (!self.courseDetail) {
        [self wsj_showLodingGif];
    }
    
    weakSelf_define;
    NSString *url = [NSString stringWithFormat:@"%@/%@",kCourseDetail_url,@(self.courseId)];
    [[HELNetworkManager sharedMg] GET:url parameters:nil success:^(id responseObject) {
        
        [weakSelf wsj_hideLodingGif];
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            weakSelf.courseDetail = [HELCourseDetailModel mj_objectWithKeyValues:responseObject[@"data"]];
            weakSelf.headerView.courseDetail = weakSelf.courseDetail;
            weakSelf.introVC.courseDetail = weakSelf.courseDetail;
            weakSelf.contentsVC.courseDetail = weakSelf.courseDetail;
            weakSelf.commentVC.courseDtail = weakSelf.courseDetail;
            
           
            if ([weakSelf.courseDetail.status containsString:@"未参加"]) {
                  [weakSelf.bottomView tellTheCurrentCourseStep:@"参加此课程" isAble:YES];
            }else if (![weakSelf.courseDetail.status containsString:@"未参加"] && !weakSelf.courseDetail.subFileName){
               
                  [weakSelf.bottomView tellTheCurrentCourseStep:nil isAble:NO];
            }else{
                  [weakSelf.bottomView tellTheCurrentCourseStep:weakSelf.courseDetail.subFileName isAble:YES];;
            }
            
            
            
        }else{
            [weakSelf wsj_showLoadFailviewWithRetry:^{
                [weakSelf getCourseDetail];
            }];
        }
 
        
    } failure:^(NSString *err) {
        [weakSelf wsj_hideLodingGif];
        [weakSelf wsj_showLoadFailviewWithRetry:^{
            [weakSelf getCourseDetail];
        }];
    }];

}

- (void)didSelectSegmentIndex:(NSInteger)index{

}

@end
